package com.hst.capacity.application.biz.impl;

import cn.hutool.core.util.StrUtil;
import com.hst.capacity.application.biz.SettingBiz;
import com.hst.capacity.domain.common.constants.Constants;
import com.hst.capacity.domain.exception.BusinessException;
import com.hst.capacity.domain.model.entity.setting.franchisee.OrderPoolSwicthEntity;
import com.hst.capacity.domain.model.entity.setting.franchisee.VirtualDriverEntity;
import com.hst.capacity.domain.model.entity.setting.price.*;
import com.hst.capacity.domain.model.request.setting.franchisee.OrderPoolSwicthParam;
import com.hst.capacity.domain.model.request.setting.franchisee.VirtualDriverParam;
import com.hst.capacity.domain.model.request.setting.price.*;
import com.hst.capacity.domain.model.response.PagingResultVO;
import com.hst.capacity.domain.model.response.setting.franchisee.OrderPoolSwicthVO;
import com.hst.capacity.domain.model.response.setting.franchisee.VirtualDriverVO;
import com.hst.capacity.domain.model.response.setting.price.*;
import com.hst.capacity.domain.model.response.specialcar.SpSettlementViewVO;
import com.hst.capacity.infra.convertor.SettingPriceConvertor;
import com.hst.capacity.infra.persistence.jpa.setting.franchisee.OrderPoolSwicthRepository;
import com.hst.capacity.infra.persistence.jpa.setting.franchisee.VirtualDriverRepository;
import com.hst.capacity.infra.persistence.jpa.setting.price.*;
import com.hst.capacity.infra.persistence.jpa.setting.station.SysRegionSettingRepository;
import com.hst.capacity.infra.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SettingBizImpl implements SettingBiz {

    @Autowired
    NewExpressPriceRepository newExpressPriceRepository;
    @Autowired
    ExCommonPriceRepository commonPriceRepository;
    @Autowired
    ExStpPriceRepository stpPriceRepository;
    @Autowired
    IcPriceRepository icPriceRepository;
    @Autowired
    SysRegionSettingRepository regionSettingRepository;
    @Autowired
    SpecialCarPriceRepository spPriceEntityRepository;
    @Autowired
    AirportPriceEntityRepository airportPriceEntityRepository;
    @Autowired
    RailwayStationPriceRepository rsPriceEntityRepository;
    @Autowired
    NewAirportPriceRepository newAirportPriceRepository;
    @Autowired
    NewStationPriceRepository newStationPriceRepository;
    @Autowired
    AirportCommonPriceRepository airportCommonPriceRepository;
    @Autowired
    StationCommonPriceRepository stationCommonPriceRepository;
    @Autowired
    SpPremiumPriceRepository spPremiumPriceRepository;
    @Autowired
    OrderPoolSwicthRepository orderPoolSwicthRepository;
    @Autowired
    VirtualDriverRepository virtualDriverRepository;
    @Autowired
    PasswordEncoder passwordEncoder;

    @Override
    public String convertPwd(String pwd) {
        return passwordEncoder.encode(pwd);
    }

    @Override
    public PagingResultVO<NewExpressPriceVO> newExpressSearch(SearchParam param) {
        Page<NewExpressPriceEntity> page = newExpressPriceRepository.findByIdFranchisee(param.getIdFranchisee(), PageRequest.of(param.getPageIndex()-1, param.getPageSize()));
        return new PagingResultVO<>(page.getTotalElements(),SettingPriceConvertor.INSTANCE.convert2NewExLst(page.getContent()));
    }

    @Override
    public PagingResultVO<ExpressSearchVO> expressSearch(SearchParam param) {
        Page<ExpressSearchVO> page = commonPriceRepository.findByIdFranchisee(param.getIdFranchisee(), PageRequest.of(param.getPageIndex()-1, param.getPageSize()));
        return new PagingResultVO<>(page.getTotalElements(),page.getContent());
    }

    @Override
    public PagingResultVO<ExpressStpSearchVO> expressStpSearch(SearchParam param) {
        Page<ExpressStpSearchVO> page = stpPriceRepository.findByIdFranchisee(param.getIdFranchisee(), PageRequest.of(param.getPageIndex()-1, param.getPageSize()));
        return new PagingResultVO<>(page.getTotalElements(),page.getContent());
    }

    @Override
    public PagingResultVO<IntercitySearchVO> intercitySearch(SearchParam param) {
        Page<IntercitySearchVO> page = icPriceRepository.findByIdFranchisee(param.getIdFranchisee(), PageRequest.of(param.getPageIndex() - 1, param.getPageSize()));
        return new PagingResultVO<>(page.getTotalElements(),page.getContent());
    }

    @Override
    public PagingResultVO<SpecialCarSearchVO> specialCarSearch(SearchParam param) {
        Page<SpecialCarSearchVO> page = spPriceEntityRepository.findByIdFranchisee(param.getIdFranchisee(), PageRequest.of(param.getPageIndex() - 1, param.getPageSize()));
        return new PagingResultVO<>(page.getTotalElements(),page.getContent());
    }

    @Override
    public PagingResultVO<AirportSearchVO> airportSearch(SearchParam param) {
        Page<AirportSearchVO> page = airportPriceEntityRepository.findByIdFranchisee(param.getIdFranchisee(), PageRequest.of(param.getPageIndex() - 1, param.getPageSize()));
        return new PagingResultVO<>(page.getTotalElements(),page.getContent());
    }

    @Override
    public PagingResultVO<RailwayStationSearchVO> railwayStationSearch(SearchParam param) {
        Page<RailwayStationSearchVO> page = rsPriceEntityRepository.findByIdFranchisee(param.getIdFranchisee(), PageRequest.of(param.getPageIndex() - 1, param.getPageSize()));
        return new PagingResultVO<>(page.getTotalElements(),page.getContent());
    }

    @Override
    public PagingResultVO<NewAirportPriceVO> newAirportSearch(SearchParam param) {
        Page<NewAirportPriceEntity> page = newAirportPriceRepository.findByIdFranchisee(param.getIdFranchisee(), PageRequest.of(param.getPageIndex() - 1, param.getPageSize()));
        return new PagingResultVO<>(page.getTotalElements(),SettingPriceConvertor.INSTANCE.convert2ApLst(page.getContent()));
    }

    @Override
    public PagingResultVO<NewAirportPriceVO> airportCommonSearch(SearchParam param) {
        Page<AirportCommonPriceEntity> page = airportCommonPriceRepository.findByIdFranchisee(param.getIdFranchisee(), PageRequest.of(param.getPageIndex() - 1, param.getPageSize()));
        return new PagingResultVO<>(page.getTotalElements(),SettingPriceConvertor.INSTANCE.convert2ApCommonLst(page.getContent()));
    }

    @Override
    public PagingResultVO<NewStationPriceVO> stationCommonSearch(SearchParam param) {
        Page<StationCommonPriceEntity> page = stationCommonPriceRepository.findByIdFranchisee(param.getIdFranchisee(), PageRequest.of(param.getPageIndex() - 1, param.getPageSize()));
        return new PagingResultVO<>(page.getTotalElements(),SettingPriceConvertor.INSTANCE.convert2StCommonLst(page.getContent()));
    }

    @Override
    public PagingResultVO<NewStationPriceVO> stationSearch(SearchParam param) {
        Page<NewStationPriceEntity> page = newStationPriceRepository.findByIdFranchisee(param.getIdFranchisee(), PageRequest.of(param.getPageIndex() - 1, param.getPageSize()));
        return new PagingResultVO<>(page.getTotalElements(),SettingPriceConvertor.INSTANCE.convert2StLst(page.getContent()));
    }

    @Override
    public String newExpressSave(NewExSaveParam param) {
        NewExpressPriceEntity entity;
        String idPrice = null;
        if (StrUtil.isNotEmpty(param.getIdPrice())){
            entity = newExpressPriceRepository.findByIdServicePriceType(param.getIdPrice());
            if (entity==null)
                throw new BusinessException(5002,"定价ID[idPrice]不存在请检查");
            idPrice = entity.getIdServicePriceType();
            if (!param.getIdFranchisee().equals(entity.getIdFranchisee()))
                throw new BusinessException(5002,"运力商编码不能随意修改");

            if (!param.getRegionCode().equals(entity.getRegionCode()))
                throw new BusinessException(5002,"地区编码不能随意修改");

            if (!param.getIdServiceType().equals(entity.getIdServiceType()))
                throw new BusinessException(5002,"服务类型不能随意修改");

            if (!(param.getIdVehicleType() == entity.getIdVehicleType()))
                throw new BusinessException(5002,"车辆类型不能随意修改");

            entity = SettingPriceConvertor.INSTANCE.convert2Entity(param);
            entity.setIdServicePriceType(idPrice);
        }else {
            entity= SettingPriceConvertor.INSTANCE.convert2Entity(param);
        }
        return newExpressPriceRepository.save(entity).getIdServicePriceType();
    }

    @Override
    public String expressSave(ExSaveParam param) {
        ExpressCommonPriceEntity entity;
        Integer idPrice;
       if (StrUtil.isNotEmpty(param.getIdServicePriceType())){
            entity=commonPriceRepository.findById(Integer.valueOf(param.getIdServicePriceType())).orElse(null);
            if (entity==null)
                throw new BusinessException(5002,"定价ID[idServicePriceType]不存在请检查");
           idPrice = entity.getIdServicePriceType();

           if (!param.getIdFranchisee().equals(entity.getIdFranchisee()))
               throw new BusinessException(5002,"运力商编码不能随意修改");

           if (!param.getRegionCode().equals(entity.getRegionCode()))
               throw new BusinessException(5002,"地区编码不能随意修改");

           if (!param.getIdServiceType().equals(entity.getIdServiceType()))
               throw new BusinessException(5002,"服务类型不能随意修改");

           if (!(param.getIdVehicleType() == entity.getIdVehicleType()))
               throw new BusinessException(5002,"车辆类型不能随意修改");
           SettingPriceConvertor.INSTANCE.convert(entity,param);
           entity.setIdServicePriceType(idPrice);
        }else {
           entity= commonPriceRepository.findByIdFranchiseeAndRegionCodeAndIdVehicleTypeAndIdServiceType(param.getIdFranchisee(), param.getRegionCode(), String.valueOf(param.getIdVehicleType()), param.getIdServiceType());
           if (entity!=null)
                throw new BusinessException(5002,"该定价场景已存在,无法新增,请前往修改");
            entity = newExpressCommonPriceEntity(param);
        }
        return commonPriceRepository.save(entity).getIdServicePriceType().toString();
    }

    @Override
    public String expressStpSave(StpSaveParam param) {
        ExpressStpPriceEntity entity;
        String idPrice = null;
        if (StrUtil.isNotEmpty(param.getIdServicePriceType())){
            //编辑
            entity=stpPriceRepository.findById(param.getIdServicePriceType()).orElse(null);
            if (entity==null)
                throw new BusinessException(5002,"定价ID[idServicePriceType]不存在请检查");
            idPrice = entity.getIdServicePriceType();

            if (!param.getIdFranchisee().equals(entity.getIdFranchisee()))
                throw new BusinessException(5002,"运力商编码不能随意修改");

            if (!param.getIdStation().equals(entity.getIdStation()))
                throw new BusinessException(5002,"站点信息不能随意修改");

            SettingPriceConvertor.INSTANCE.convert(entity,param);
            entity.setIdServicePriceType(idPrice);
        }else {
            //添加
            entity = stpPriceRepository.findByIdFranchiseeAndIdStationAndIdVehicleTypeAndCarpoolType(param.getIdFranchisee(), param.getIdStation(), param.getIdVehicleType(), param.getCarpoolType());
            if (entity!=null)
                throw new BusinessException(5002,"该定价场景已存在,无法新增,请前往修改");
            entity = newExpressStpPriceEntity(param);
        }
        return stpPriceRepository.save(entity).getIdServicePriceType();
    }

    @Override
    public String intercitySave(IcSaveParam param) {
        IntercityPriceEntity entity;
        Integer idPrice = null;
        if (param.getIdIntercityLinePrice()!=null){
            //编辑
            entity=icPriceRepository.findById(param.getIdIntercityLinePrice()).orElse(null);

            if (entity==null)
                throw new BusinessException(5002,"定价ID[idIntercityLinePrice]不存在请检查");
            idPrice = entity.getIdIntercityLinePrice();

            if (!param.getIdFranchisee().equals(entity.getIdFranchisee()))
                throw new BusinessException(5002,"运力商编码不能随意修改");

            if (!param.getIdLine().equals(entity.getIdLine()))
                throw new BusinessException(5002,"线路编码不能随意修改");

            if (!param.getIdServiceType().equals(entity.getIdServiceType()))
                throw new BusinessException(5002,"服务类型不能随意修改");

            SettingPriceConvertor.INSTANCE.convert(entity,param);
            entity.setIdIntercityLinePrice(idPrice);
        }else {//添加
            entity=icPriceRepository.findByIdFranchiseeAndIdLine(param.getIdFranchisee(), param.getIdLine());
            if (entity!=null)
                throw new BusinessException(5002,"该定价场景已存在,无法新增,请前往修改");
            entity=newIntercityPriceEntity(param);
        }
        return icPriceRepository.save(entity).getIdIntercityLinePrice().toString();
    }

    @Override
    public String specialCarSave(SpSaveParam param) {
        SpecialCarPriceEntity entity;
        String idPrice = null;
        if (StrUtil.isNotEmpty(param.getIdServicePriceType())){
            //编辑
            entity = spPriceEntityRepository.findById(param.getIdServicePriceType()).orElse(null);

            if (entity == null)
                throw new BusinessException(5002,"定价ID[idServicePriceType]不存在请检查");
            idPrice = entity.getIdServicePriceType();

            if (!param.getIdFranchisee().equals(entity.getIdFranchisee()))

                throw new BusinessException(5002,"运力商编码不能随意修改");

            if (!param.getRegionCode().equals(entity.getRegionCode()))
                throw new BusinessException(5002,"地区编码不能随意修改");

            if (!param.getIdServiceType().equals(entity.getIdServiceType()))
                throw new BusinessException(5002,"服务类型不能随意修改");

            SettingPriceConvertor.INSTANCE.convert(entity, param);
            entity.setIdServicePriceType(idPrice);
        }else {
            //添加
            entity = spPriceEntityRepository.findByIdFranchiseeAndRegionCodeAndIdVehicleTypeAndIdServiceType(param.getIdFranchisee(), param.getRegionCode(), String.valueOf(param.getIdVehicleType()), param.getIdServiceType());
            if (entity != null)
                throw new BusinessException(5002,"该定价场景已存在,无法新增,请前往修改");
            entity = newSpecialCarPriceEntity(param);
        }
        return spPriceEntityRepository.save(entity).getIdServicePriceType().toString();
    }

    @Override
    public String airportSave(NewApParam param) {
        NewAirportPriceEntity newAirportPriceEntity = newAirportPriceRepository
                .findAllByIdAirportAndIdServicePriceTypeAndChannel(param.getIdFranchisee(),
                        param.getIdAirport(),param.getIdServiceType(),param.getIdVehicleType(),param.getChannel());
        String idServicePriceType = null;
        String markupPrice = null;
        boolean isExist = false;
        if (newAirportPriceEntity != null) {
            idServicePriceType = newAirportPriceEntity.getIdServicePriceType();
            markupPrice = newAirportPriceEntity.getMarkupPriceSetting();
            isExist = true;
        }
        newAirportPriceEntity = SettingPriceConvertor.INSTANCE.convert2Entity(param);
        if (isExist) {
            newAirportPriceEntity.setIdServicePriceType(idServicePriceType);
            newAirportPriceEntity.setMarkupPriceSetting(markupPrice);
        }
        return newAirportPriceRepository.save(newAirportPriceEntity).getIdServicePriceType();
    }

    @Override
    public String airportCommonSave(ApCommonParam param) {
        AirportCommonPriceEntity airportCommonPriceEntity = airportCommonPriceRepository
                .findAllByIdAirportAndIdServicePriceTypeAndChannel(param.getIdFranchisee(),
                        param.getIdAirport(),param.getIdServiceType(),param.getIdVehicleType(),param.getChannel());
        String idServicePriceType = null;
        String markupPrice = null;
        boolean isExist = false;
        if (airportCommonPriceEntity != null) {
            idServicePriceType = airportCommonPriceEntity.getIdServicePriceType();
            markupPrice = airportCommonPriceEntity.getMarkupPriceSetting();
            isExist = true;
        }
        airportCommonPriceEntity = SettingPriceConvertor.INSTANCE.convert2Entity(param);
        if (isExist) {
            airportCommonPriceEntity.setIdServicePriceType(idServicePriceType);
            airportCommonPriceEntity.setMarkupPriceSetting(markupPrice);
        }
        return airportCommonPriceRepository.save(airportCommonPriceEntity).getIdServicePriceType();
    }

    @Override
    public String stationCommonSave(StCommonParam param) {
        StationCommonPriceEntity stationCommonPriceEntity = stationCommonPriceRepository
                .findAllByIdStation(param.getIdFranchisee(),
                        param.getIdStation(),param.getIdServiceType(),param.getIdVehicleType(),param.getChannel());
        String idServicePriceType = null;
        if (stationCommonPriceEntity != null)
            idServicePriceType = stationCommonPriceEntity.getIdServicePriceType();
        stationCommonPriceEntity = SettingPriceConvertor.INSTANCE.convert2Entity(param);
        if (stationCommonPriceEntity != null) {
            stationCommonPriceEntity.setIdServicePriceType(idServicePriceType);
        }
        return stationCommonPriceRepository.save(stationCommonPriceEntity).getIdServicePriceType();
    }

    @Override
    public String stationSave(NewStParam param) {
        NewStationPriceEntity newStationPriceEntity = newStationPriceRepository
                .findAllByIdStation(param.getIdFranchisee(),
                param.getIdStation(),param.getIdServiceType(),param.getIdVehicleType(),param.getChannel());
        String idServicePriceType = null;
        if (newStationPriceEntity != null)
            idServicePriceType = newStationPriceEntity.getIdServicePriceType();
        newStationPriceEntity = SettingPriceConvertor.INSTANCE.convert2Entity(param);
        if (newStationPriceEntity != null) {
            newStationPriceEntity.setIdServicePriceType(idServicePriceType);
        }
        return newStationPriceRepository.save(newStationPriceEntity).getIdServicePriceType();
    }

    @Override
    public String newExpressStatus(String idPrice, String status) {
        NewExpressPriceEntity entity = newExpressPriceRepository.findByIdServicePriceType(idPrice);
        if (entity == null)
            throw new BusinessException(5002,"定价ID[idPrice]不存在请检查");
        entity.setStatus(Integer.valueOf(status));
        return newExpressPriceRepository.save(entity).getIdServicePriceType();
    }

    @Override
    public String airportStatus(String idPrice, String status) {
        NewAirportPriceEntity entity = newAirportPriceRepository.findById(idPrice).orElse(null);
        if (entity == null)
            throw new BusinessException(5002,"定价ID[idPrice]不存在请检查");
        entity.setStatus(Integer.valueOf(status));
        return newAirportPriceRepository.save(entity).getIdServicePriceType();
    }

    @Override
    public String stationStatus(String idPrice, String status) {
        NewStationPriceEntity entity = newStationPriceRepository.findById(idPrice).orElse(null);
        if (entity == null)
            throw new BusinessException(5002,"定价ID[idPrice]不存在请检查");
        entity.setStatus(Integer.valueOf(status));
        return newStationPriceRepository.save(entity).getIdServicePriceType();
    }

    @Override
    public String airportCommonStatus(String idPrice, String status) {
        AirportCommonPriceEntity entity = airportCommonPriceRepository.findById(idPrice).orElse(null);
        if (entity == null)
            throw new BusinessException(5002,"定价ID[idPrice]不存在请检查");
        entity.setStatus(Integer.valueOf(status));
        return airportCommonPriceRepository.save(entity).getIdServicePriceType();
    }

    @Override
    public String stationCommonStatus(String idPrice, String status) {
        StationCommonPriceEntity entity = stationCommonPriceRepository.findById(idPrice).orElse(null);
        if (entity == null)
            throw new BusinessException(5002,"定价ID[idPrice]不存在请检查");
        entity.setStatus(Integer.valueOf(status));
        return stationCommonPriceRepository.save(entity).getIdServicePriceType();
    }

    @Override
    public IntercityPriceVO intercityPrice(String idPrice) {
        IntercityPriceVO vo = icPriceRepository.findByIdPrice(Integer.valueOf(idPrice));
        if (vo == null)
            throw new BusinessException(5002,"定价记录未找到或者配置不完整,请检查");
        return vo;
    }

    @Override
    public IntercityPriceVO intercityPriceByIdline(String idLine) {
        IntercityPriceVO vo = icPriceRepository.findBbyIdLine(idLine);
        if (vo == null)
            throw new BusinessException(5002,"定价记录未找到或者配置不完整,请检查");
        return vo;
    }

    @Override
    public NewExpressPriceVO newExpressPrice(String idPrice) {
        return SettingPriceConvertor.INSTANCE.convertNewExPrice(newExpressPriceRepository.findByIdServicePriceType(idPrice));
    }

    @Override
    public String newExpressMarkup(String idPrice) {
        NewExpressPriceEntity entity = newExpressPriceRepository.findMarkupByIdServicePriceType(idPrice);
        if (entity == null)
            throw new BusinessException(5002,"未找到定价ID[idPrice]的数据,请检查");
        else
            return entity.getMarkupPriceContent();
    }

    @Override
    public String newExpressMarkupSave(String idPrice, String markup) {
        NewExpressPriceEntity entity = newExpressPriceRepository.findMarkupByIdServicePriceType(idPrice);
        if (entity == null)
            throw new BusinessException(5002,"未找到定价ID[idPrice]的数据,请检查");
        entity.setMarkupPriceContent(markup);
        return newExpressPriceRepository.save(entity).getIdServicePriceType();
    }

    @Override
    public ExpressPriceVO expressPrice(String idPrice) {
        return SettingPriceConvertor.INSTANCE.convertExPrice(commonPriceRepository.findById(Integer.valueOf(idPrice)).get());
    }

    @Override
    public ExpressStpPriceVO expressStpPrice(String idPrice) {
        return SettingPriceConvertor.INSTANCE.convertStpPrice(stpPriceRepository.findById(idPrice).get());
    }

    @Override
    public SpecialCarPriceVO specialCarPrice(String idPrice) {
        return spPriceEntityRepository.findByIdPrice(idPrice);
    }

    @Override
    public AirportPriceVO airportPrice(String idPrice) {
        return SettingPriceConvertor.INSTANCE.convertAirportPrice(airportPriceEntityRepository.findById(idPrice).orElse(null));
    }

    @Override
    public RailwayStationPriceVO railwayStationPrice(String idPrice) {
        return SettingPriceConvertor.INSTANCE.convertRailwayStationPrice(rsPriceEntityRepository.findById(idPrice).orElse(null));
    }

    @Override
    public NewAirportPriceVO newAirportPrice(String idPrice) {
        return SettingPriceConvertor.INSTANCE.convertNewAirportPrice(newAirportPriceRepository.findById(idPrice).orElse(null));
    }

    @Override
    public String newAirportMarkup(String idPrice) {
        NewAirportPriceEntity entity = newAirportPriceRepository.findMarkupByIdServicePriceType(idPrice);
        if (entity == null)
            throw new BusinessException(5002,"未找到定价ID[idPrice]的数据,请检查");
        else
            return entity.getMarkupPriceSetting();
    }

    @Override
    public String newAirportMarkupSave(String idPrice, String markup) {
        NewAirportPriceEntity entity = newAirportPriceRepository.findMarkupByIdServicePriceType(idPrice);
        if (entity == null)
            throw new BusinessException(5002,"未找到定价ID[idPrice]的数据,请检查");
        entity.setMarkupPriceSetting(markup);
        return newAirportPriceRepository.save(entity).getIdServicePriceType();
    }

    @Override
    public NewAirportPriceVO airportCommonDetail(String idPrice) {
        return SettingPriceConvertor.INSTANCE.convertNewAirportPrice(airportCommonPriceRepository.findByIdServicePriceType(idPrice));
    }

    @Override
    public String airportCommonMarkup(String idPrice) {
        AirportCommonPriceEntity entity = airportCommonPriceRepository.findMarkupByIdServicePriceType(idPrice);
        if (entity == null)
            throw new BusinessException(5002,"未找到定价ID[idPrice]的数据,请检查");
        else
            return entity.getMarkupPriceSetting();
    }

    @Override
    public String airportCommonMarkupSave(String idPrice, String markup) {
        AirportCommonPriceEntity entity = airportCommonPriceRepository.findMarkupByIdServicePriceType(idPrice);
        if (entity == null)
            throw new BusinessException(5002,"未找到定价ID[idPrice]的数据,请检查");
        entity.setMarkupPriceSetting(markup);
        return airportCommonPriceRepository.save(entity).getIdServicePriceType();
    }

    @Override
    public NewStationPriceVO stationCommonDetail(String idPrice) {
        return SettingPriceConvertor.INSTANCE.convertNewStationPrice(stationCommonPriceRepository.findById(idPrice).orElse(null));
    }

    @Override
    public String stationCommonMarkup(String idPrice) {
        StationCommonPriceEntity entity = stationCommonPriceRepository.findMarkupByIdServicePriceType(idPrice);
        if (entity == null)
            throw new BusinessException(5002,"未找到定价ID[idPrice]的数据,请检查");
        else
            return entity.getMarkupPriceSetting();
    }

    @Override
    public String stationCommonMarkupSave(String idPrice, String markup) {
        StationCommonPriceEntity entity = stationCommonPriceRepository.findMarkupByIdServicePriceType(idPrice);
        if (entity == null)
            throw new BusinessException(5002,"未找到定价ID[idPrice]的数据,请检查");
        entity.setMarkupPriceSetting(markup);
        return stationCommonPriceRepository.save(entity).getIdServicePriceType();
    }

    @Override
    public NewStationPriceVO newStationPrice(String idPrice) {
        return SettingPriceConvertor.INSTANCE.convertNewStationPrice(newStationPriceRepository.findById(idPrice).orElse(null));
    }

    @Override
    public String newStationMarkup(String idPrice) {
        NewStationPriceEntity entity = newStationPriceRepository.findMarkupByIdServicePriceType(idPrice);
        if (entity == null)
            return null;
        else
            return entity.getMarkupPriceSetting();
    }

    @Override
    public String newStationMarkupSave(String idPrice, String markup) {
        NewStationPriceEntity entity = newStationPriceRepository.findMarkupByIdServicePriceType(idPrice);
        if (entity == null)
            throw new BusinessException(5002,"未找到定价ID[idPrice]的数据,请检查");
        entity.setMarkupPriceSetting(markup);
        return newStationPriceRepository.save(entity).getIdServicePriceType();
    }

    @Override
    public ExSaveParam expressSelect(String idPrice) {
        ExSaveParam re = SettingPriceConvertor.INSTANCE.convertExSelect(commonPriceRepository.findById(Integer.valueOf(idPrice)).orElse(null));
        if (re==null)
            throw new BusinessException(5002,"定价ID[idPrice]不存在,请检查");
        return re;
    }

    @Override
    public StpSaveParam expressStpSelect(String idPrice) {
        StpSaveParam re = SettingPriceConvertor.INSTANCE.convertStpSelect(stpPriceRepository.findById(idPrice).orElse(null));
        if (re==null)
            throw new BusinessException(5002,"定价ID[idPrice]不存在,请检查");
        return re;
    }

    @Override
    public IcSaveParam intercitySelect(String idPrice) {
        IcSaveParam re = SettingPriceConvertor.INSTANCE.convertIcSelect(icPriceRepository.findById(Integer.valueOf(idPrice)).orElse(null));
        if (re==null)
            throw new BusinessException(5002,"定价ID[idPrice]不存在,请检查");
        return re;
    }

    @Override
    public SpSaveParam specialCarSelect(String idPrice) {
        SpSaveParam re = SettingPriceConvertor.INSTANCE.convertSpSelect(spPriceEntityRepository.findById(idPrice).orElse(null));
        if (re==null)
            throw new BusinessException(5002,"定价ID[idPrice]不存在,请检查");
        return re;
    }

    @Override
    public PagingResultVO<SpPremiumPriceVO> spAllPremiumPriceSelect(SpPremiumPriceParam param) {
        Page<SpPremiumPriceEntity> page = spPremiumPriceRepository.findByIdFranchiseeAll(param.getIdFranchisee(), PageRequest.of(param.getPageIndex() - 1, param.getPageSize()));
        return new PagingResultVO<>(page.getTotalElements(), SettingPriceConvertor.INSTANCE.convert2SpPremiumLstVO(page.getContent()));
    }

    @Override
    public List<SpPremiumPriceVO> spPremiumPriceSelect(SpPremiumPriceParam param) {
        List<SpPremiumPriceEntity> list = null;
        if (StringUtil.isNotBlank(param.getIdPrice()))
            list = spPremiumPriceRepository.findByIdPriceAll(param.getIdPrice());
        return SettingPriceConvertor.INSTANCE.convert2SpPremiumLstVO(list);
    }

    @Override
    public SpPremiumPriceVO spPremiumPriceDetail(String idPremiumPrice) {
        return SettingPriceConvertor.INSTANCE.convert2VO(spPremiumPriceRepository.findByIdPremiumPrice(idPremiumPrice));
    }

    @Override
    public String spPremiumPriceSave(SpPremiumPriceParam param) {
        SpPremiumPriceEntity entity = SettingPriceConvertor.INSTANCE.convert2Entity(param);
        return spPremiumPriceRepository.save(entity).getIdPremiumPrice();
    }

    @Override
    public List<OrderPoolSwicthVO> orderPoolSwicthSelect(OrderPoolSwicthParam param) {
        return SettingPriceConvertor.INSTANCE.convert2Lst(orderPoolSwicthRepository.findByIdFranchisee(param.getIdFranchisee()));
    }

    @Override
    public String orderPoolSwicthSave(OrderPoolSwicthParam param) {
        OrderPoolSwicthEntity entity;
        if (StringUtil.isNotBlank(param.getIdOrderPoolSwicth())) {
            entity = orderPoolSwicthRepository.findByIdOrderPoolSwicth(param.getIdOrderPoolSwicth());
            if (entity == null) {
                throw new BusinessException(5002,"主键ID不存在,请检查");
            } else {
                if (param.getMoudleType() != null)
                    entity.setModuleType(param.getMoudleType());
                if (param.getChannel() != null)
                    entity.setChannel(param.getChannel());
                if (param.getStatus() != null)
                    entity.setStatus(param.getStatus());
            }
        } else {
            entity = SettingPriceConvertor.INSTANCE.convert2Entity(param);
            if (param.getMoudleType() == null)
                entity.setModuleType(Constants.OrderModuleType.SPECIAL);
            if (param.getChannel() == null)
                entity.setChannel(Integer.parseInt(Constants.Channel.ALL));
        }
        return orderPoolSwicthRepository.save(entity).getIdOrderPoolSwicth();
    }

    @Override
    public List<VirtualDriverVO> virtualDriverLst(VirtualDriverParam param) {
        List<VirtualDriverEntity> lst = virtualDriverRepository.findByIdFranchisee(param.getIdFranchisee());
        return SettingPriceConvertor.INSTANCE.convert2VO(lst);
    }

    @Override
    public VirtualDriverVO virtualDriverInfo(String id) {
        VirtualDriverEntity entity = virtualDriverRepository.findByIdVirtualDriver(id);
        return SettingPriceConvertor.INSTANCE.convert2VO(entity);
    }

    @Override
    public String virtualDriverSave(VirtualDriverParam param) {
        VirtualDriverEntity entity;
        if (StringUtil.isNotBlank(param.getIdVirtualDriver())) {
            entity = virtualDriverRepository.findByIdVirtualDriver(param.getIdVirtualDriver());
            if (entity == null) {
                throw new BusinessException(5002,"主键ID不存在,请检查");
            } else {
                if (param.getModuleType() != null)
                    entity.setModuleType(param.getModuleType());
                if (param.getIdDriver() != null)
                    entity.setIdDriver(param.getIdDriver());
                if (param.getIdCityLine() != null)
                    entity.setIdCityLine(param.getIdCityLine());
                if (param.getBizType() != null)
                    entity.setBizType(param.getBizType());
                if (param.getDriverName() != null)
                    entity.setDriverName(param.getDriverName());
                if (param.getDriverMobile() != null)
                    entity.setDriverMobile(param.getDriverMobile());
                if (param.getPlateNumber() != null)
                    entity.setPlateNumber(param.getPlateNumber());
                if (param.getVehicleColor() != null)
                    entity.setVehicleColor(param.getVehicleColor());
                if (param.getVehicleBrand() != null)
                    entity.setVehicleBrand(param.getVehicleBrand());
                if (param.getVehicleModel() != null)
                    entity.setVehicleModel(param.getVehicleModel());
                if (param.getWorkTime() != null)
                    entity.setWorkTime(param.getWorkTime());
                if (param.getChannel() == null)
                    entity.setChannel(Integer.parseInt(Constants.Channel.ALL));
                else
                    entity.setChannel(param.getChannel());
            }
        } else {
            entity = SettingPriceConvertor.INSTANCE.convert2Entity(param);
            if (param.getChannel() == null)
                entity.setChannel(Integer.parseInt(Constants.Channel.ALL));
        }
        return virtualDriverRepository.save(entity).getIdVirtualDriver();
    }

    private ExpressStpPriceEntity newExpressStpPriceEntity(StpSaveParam param){
        ExpressStpPriceEntity expressStpPriceEntity = new ExpressStpPriceEntity();
//        expressStpPriceEntity.setIdServicePriceType(param.getIdServicePriceType());
        expressStpPriceEntity.setRegionCode(param.getRegionCode());
        expressStpPriceEntity.setIdFranchisee(param.getIdFranchisee());
        expressStpPriceEntity.setIdStation(param.getIdStation());
        expressStpPriceEntity.setCarpoolType(param.getCarpoolType());
        expressStpPriceEntity.setIdVehicleType(param.getIdVehicleType());
        expressStpPriceEntity.setSeats(param.getSeats());
        expressStpPriceEntity.setWeekday(param.getWeekday());
        expressStpPriceEntity.setStartingKilometers(param.getStartingKilometers());
        expressStpPriceEntity.setStartingFee(param.getStartingFee());
        expressStpPriceEntity.setBaseKilometers(param.getBaseKilometers());
        expressStpPriceEntity.setBaseFee(param.getBaseFee());
        expressStpPriceEntity.setLongDistanceKilometers(param.getLongDistanceKilometers());
        expressStpPriceEntity.setLongDistanceFee(param.getLongDistanceFee());
        expressStpPriceEntity.setOverMileageFee(param.getOverMileageFee());
        expressStpPriceEntity.setDurationFee(param.getDurationFee());
//        expressStpPriceEntity.setMemo();
//        expressStpPriceEntity.setCreateBy();
//        expressStpPriceEntity.setCreateTime();
//        expressStpPriceEntity.setUpdateBy();
//        expressStpPriceEntity.setUpdateTime();
        return expressStpPriceEntity;
    }

    private ExpressCommonPriceEntity newExpressCommonPriceEntity(ExSaveParam param){
        ExpressCommonPriceEntity expressCommonPriceEntity = new ExpressCommonPriceEntity();
//        expressCommonPriceEntity.setIdServicePriceType(param.getIdServicePriceType());
        expressCommonPriceEntity.setRegionCode(param.getRegionCode());
        expressCommonPriceEntity.setIdFranchisee(param.getIdFranchisee());
        expressCommonPriceEntity.setIdVehicleType(param.getIdVehicleType());
        expressCommonPriceEntity.setIdServiceType(param.getIdServiceType());
        expressCommonPriceEntity.setServicePrice(param.getServicePrice());
        expressCommonPriceEntity.setMaxServiceMileage(param.getMaxServiceMileage());
        expressCommonPriceEntity.setSecondServiceMileage(param.getSecondServiceMileage());
        expressCommonPriceEntity.setSecondMileagePrice(param.getSecondMileagePrice());
        expressCommonPriceEntity.setThirdServiceMileage(param.getThirdServiceMileage());
        expressCommonPriceEntity.setThirdMileagePrice(param.getThirdMileagePrice());
        expressCommonPriceEntity.setFourthServiceMileage(param.getFourthServiceMileage());
        expressCommonPriceEntity.setFourthMileagePrice(param.getFourthMileagePrice());
        expressCommonPriceEntity.setExceedingMileagePrice(param.getExceedingMileagePrice());
        expressCommonPriceEntity.setMaxWaitingTime(param.getMaxWaitingTime());
        expressCommonPriceEntity.setMaxServiceTime(param.getMaxServiceTime());
        expressCommonPriceEntity.setExceedingTimePrice(param.getExceedingTimePrice());
        expressCommonPriceEntity.setWaitingTimePrice(param.getWaitingTimePrice());
        expressCommonPriceEntity.setNightServiceTime(param.getNightServiceTime());
        expressCommonPriceEntity.setNightServiceAddition(param.getNightServiceAddition());
        expressCommonPriceEntity.setNightServicePrice(param.getNightServicePrice());
        expressCommonPriceEntity.setMorningPeakTime(param.getMorningPeakTime());
        expressCommonPriceEntity.setMorningPeakAddition(param.getMorningPeakAddition());
        expressCommonPriceEntity.setMorningPeakPrice(param.getMorningPeakPrice());
        expressCommonPriceEntity.setEveningPeakTime(param.getEveningPeakTime());
        expressCommonPriceEntity.setEveningPeakAddition(param.getEveningPeakAddition());
        expressCommonPriceEntity.setEveningPeakPrice(param.getEveningPeakPrice());
//        expressCommonPriceEntity.setMemo();
//        expressCommonPriceEntity.setCreateBy();
//        expressCommonPriceEntity.setCreateTime();
//        expressCommonPriceEntity.setUpdateBy();
//        expressCommonPriceEntity.setUpdateTime();
        return expressCommonPriceEntity;
    }

    private IntercityPriceEntity newIntercityPriceEntity(IcSaveParam param){
        IntercityPriceEntity intercityPriceEntity = new IntercityPriceEntity();
//        intercityPriceEntity.setIdIntercityLinePrice(param.getIdIntercityLinePrice());
        intercityPriceEntity.setIdLine(param.getIdLine());
        intercityPriceEntity.setIdFranchisee(param.getIdFranchisee());
        intercityPriceEntity.setIdVehicleType(param.getIdVehicleType());
        intercityPriceEntity.setIdServiceType(param.getIdServiceType());
        intercityPriceEntity.setServicePrice(param.getServicePrice());
        intercityPriceEntity.setCharteredCarPrice(param.getCharteredCarPrice());
        intercityPriceEntity.setCharteredCarSeat(param.getCharteredCarSeat());
        intercityPriceEntity.setMaxServiceMileage(param.getMaxServiceMileage());
        intercityPriceEntity.setExceedingMileagePrice(param.getExceedingMileagePrice());
        intercityPriceEntity.setMaxWaitingTime(param.getMaxWaitingTime());
        intercityPriceEntity.setWaitingTimePrice(param.getWaitingTimePrice());
        intercityPriceEntity.setExpressWayPrice(param.getExpressWayPrice());
        intercityPriceEntity.setCalculationType(param.getCalculationType());
        intercityPriceEntity.setHighSpeedBudgetFlag(param.getHighSpeedBudgetFlag());
        intercityPriceEntity.setMemo(param.getMemo());
        intercityPriceEntity.setCarrierPrice(param.getCarrierPrice());
//        intercityPriceEntity.setCreateBy();
//        intercityPriceEntity.setCreateTime();
//        intercityPriceEntity.setUpdateBy();
//        intercityPriceEntity.setUpdateTime();
        return intercityPriceEntity;
    }

    private SpecialCarPriceEntity newSpecialCarPriceEntity(SpSaveParam param) {
        SpecialCarPriceEntity specialCarPriceEntity = new SpecialCarPriceEntity();
//        specialCarPriceEntity.setIdServicePriceType(param.getIdServicePriceType());
        specialCarPriceEntity.setIdFranchisee(param.getIdFranchisee());
        specialCarPriceEntity.setIdVehicleType(param.getIdVehicleType());
        specialCarPriceEntity.setIdServiceType(param.getIdServiceType());
        specialCarPriceEntity.setRegionCode(param.getRegionCode());
        specialCarPriceEntity.setServicePrice(param.getServicePrice());
        specialCarPriceEntity.setMaxServiceMileage(param.getMaxServiceMileage());
        specialCarPriceEntity.setSecondServiceMileage(param.getSecondServiceMileage());
        specialCarPriceEntity.setThirdServiceMileage(param.getThirdServiceMileage());
        specialCarPriceEntity.setFourthServiceMileage(param.getFourthServiceMileage());
        specialCarPriceEntity.setExceedingMileagePrice(param.getExceedingMileagePrice());
        specialCarPriceEntity.setSecondMileagePrice(param.getSecondMileagePrice());
        specialCarPriceEntity.setThirdMileagePrice(param.getThirdMileagePrice());
        specialCarPriceEntity.setFourthMileagePrice(param.getFourthMileagePrice());
        specialCarPriceEntity.setMaxWaitingTime(param.getMaxWaitingTime());
        specialCarPriceEntity.setMaxServiceTime(param.getMaxServiceTime());
        specialCarPriceEntity.setExceedingTimePrice(param.getExceedingTimePrice());
        specialCarPriceEntity.setWaitingTimePrice(param.getWaitingTimePrice());
        specialCarPriceEntity.setNightServiceTime(param.getNightServiceTime());
        specialCarPriceEntity.setNightServiceAddition(param.getNightServiceAddition());
        specialCarPriceEntity.setNightServicePrice(param.getNightServicePrice());
        specialCarPriceEntity.setMorningPeakTime(param.getMorningPeakTime());
        specialCarPriceEntity.setMorningPeakAddition(param.getMorningPeakAddition());
        specialCarPriceEntity.setMorningPeakPrice(param.getMorningPeakPrice());
        specialCarPriceEntity.setEveningPeakTime(param.getEveningPeakTime());
        specialCarPriceEntity.setEveningPeakAddition(param.getEveningPeakAddition());
        specialCarPriceEntity.setEveningPeakPrice(param.getEveningPeakPrice());
//        specialCarPriceEntity.setMemo();
//        specialCarPriceEntity.setCreateBy();
//        specialCarPriceEntity.setCreateTime();
//        specialCarPriceEntity.setUpdateBy();
//        specialCarPriceEntity.setUpdateTime();
        return specialCarPriceEntity;
    }

}
